package com.example.sls.service.impl.account;

import com.example.sls.mapper.account.AccountMapper;
import com.example.sls.mapper.student.StudentMapper;
import com.example.sls.mapper.teacher.TeacherMapper;
import com.example.sls.pojo.account.Account;
import com.example.sls.pojo.student.Student;
import com.example.sls.pojo.teacher.Teacher;
import com.example.sls.service.account.RegisterService;
import com.example.sls.service.student.StudentService;
import com.example.sls.service.teacher.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Map<String, String> register(String username, String phone, String email,  String password,    String confirmedPassword,  String roleId) {
        Map<String, String> map = new HashMap<>();
//        System.out.println("========");
//        System.out.println("username : " + username);
//        System.out.println("phone : " + phone);
//        System.out.println("email : " + email);
//        System.out.println("password : " + password);
//        System.out.println("confirmedPassword : " + confirmedPassword);
//        System.out.println("roleId : " + roleId);
//        System.out.println("========");

        if (username == null) {
            map.put("error_message", "用户名不能为空");
            return map;
        }
        if (password == null || confirmedPassword == null) {
            map.put("error_message", "密码不能为空");
            return map;
        }

        username = username.trim();
        if (username.length() == 0) {
            map.put("error_message", "用户名不能为空");
            return map;
        }

        if (phone.length() == 0) {
            map.put("error_message", "手机号不能为空");
            return map;
        }

        if (password.length() == 0 || confirmedPassword.length() == 0) {
            map.put("error_message", "密码不能为空");
            return map;
        }

        if (username.length() > 20) {
            map.put("error_message", "用户名长度不能大于20");
            return map;
        }

        if (password.length() < 6 || confirmedPassword.length() < 6) {
            map.put("error_message", "密码长度不能小于6");
            return map;
        }

        if (password.length() > 100 || confirmedPassword.length() > 100) {
            map.put("error_message", "密码长度不能大于100");
            return map;
        }

        if (!password.equals(confirmedPassword)) {
            map.put("error_message", "两次输入的密码不一致");
            return map;
        }

        if(roleId == null){
            map.put("error_message", "您必须选择你的角色");
            return map;
        }


        Account account = accountMapper.getAccountByUserName(username);

        if (account != null) {
            map.put("error_message", "用户名已存在");
            return map;
        }
        if(email.equals("")){
            email = null;
        }
        String encodedPassword = passwordEncoder.encode(password);

        int id = accountMapper.getMaxId() + 1;
        int rid = Integer.parseInt(roleId);
        Account user =
                new Account(id, username, phone, email, encodedPassword,rid);

//            Map<String, Object> user = new HashMap<>();
//
//            user.put("id", accountMapper.getCount() + 1);
//            user.put("username",username);
//            user.put("password",encodedPassword);
//            user.put("roleId", roleId);

            accountMapper.addAccount(user);


            if(rid == 1){  //如果当前账号的角色是教师,则将信息添加到教师表中
                Integer Id = teacherService.getMaxId() + 1;
                Integer teacherId = id;
                String name = username;
                Teacher teacher = new Teacher(Id,teacherId,name);
                if(!teacherService.addTeacher(teacher)){
                    map.put("error_message", "add error");
                    return map;
                }
            }
            if(rid == 2){  //入过当前账号的角色是学生,则将信息添加到学生表中
                Integer Id = studentService.getMaxId() + 1;
                Integer studentId = id;
                String name = username;
                Integer cId = -1;
                Student student = new Student(Id,studentId,name,cId);
                 if( !studentService.addStudent(student)){
                     map.put("error_message", "add error");
                     return map;
                 }
            }

        map.put("error_message", "success");
        return map;
    }
}

